$(function () {
	// 返回
	var subForumId;
	$("#goback").click(function () {
		history.go(-1);
	});
	// 顶部提示栏事件
	$(".rTitle").slideDown();
	setTimeout(function () {
		$(".rTitle").text("请良性吐槽");
	}, 2000)
	setTimeout(function () {
		$(".rTitle").slideUp();
	}, 4000);

	$(".selectTitle").on("click", function () {
		if (!$(this).data("open")) {
			$(".rightTitle").slideDown();
			$(this).data("open", true);
		} else {
			$(".rightTitle").slideUp();
			$(this).data("open", false);
		}
	});
	$(".rightTitle").on("click", ".sectionTitle", function (event) {
		event.stopImmediatePropagation();
		$(this).find("ul").toggle(500).end().siblings().find("ul").hide();
	});
	$(".rightTitle").on("click", ".themeTitle", function (event) {
		event.stopImmediatePropagation();
		$(".selectTitle").text($(this).text()).data("open", false).data("themeId", $(this).data("themeId"));
		$(".rightTitle").slideUp();
		subForumId = $(this).data('themed');
	});

	$(".sContents").on("click",function(event){
		event.stopImmediatePropagation();
			$(".rightTitle").slideUp();
			$(".selectTitle").data("open", false);
	});
//	function setP() {
//		var vx = ($(window).width() - $(".slc").width()) / 2
//		$(".slc").css("left", vx);
//	}
//	setP();
//	setTimeout(setP,500);
//	$(window).resize(setP);
	$(".eImg").each(function (index) {
		index = index + 1;
		var imgUrl = "images/expression/e" + index + ".png";
		$(this).find("img").attr("src", imgUrl);
	});

	function navDown(num) {
		$("footer").slideUp();
		$(".imgBC").slideUp();
		setTimeout(function () {
			$(".eu").slideDown();
		}, 600)
		if (num) {
			$(".uploadImg").hide();
			$(".eu ul").show();
		} else {
			$(".eu ul").hide();
			$(".uploadImg").show();

		}
	}

	function navUp() {
		$(".eu").slideUp();
		setTimeout(function () {
			if ($(".imgBC img").attr("src")) {
				$(".imgBC").slideDown();
			}
			$("footer").slideDown();
		}, 600);
	}
	$(".expression").data("ex", true).on("click", function () {
		navDown(1)
	})
	$(".closeEu").on("click", navUp);
	$(".uploadPhoto").on("click", function () {
		if (!$(".imgBC img").attr("src")) {
			navDown(0);
		} else {
			$(".rTitle").text("只能上传一张图片").slideDown();
			setTimeout(function () {
				$(".rTitle").slideUp();
			}, 3000);
		}
	});

	$(".clickIF").on("click", function () {
		$("input:file").click();
	});

	$("input:file").on("change", function () {
		var file = $(this)[0].files[0];
		$(".ul img").attr("src", URL.createObjectURL(file));
	});

	$(".eImg").on("click", function () {
		$(".sContents").append("<img src=" + $(this).find("img").attr("src") + "></img>");
	})

	$(".focusOut").on("click", function () {
		$(".sContents").empty();
		$("html,body").animate({
			scrollTop: 0
		}, 500);
	});


	/********************** 请求  ************************/;
	$.get("/forum", function (resData) {
		console.log(resData);
		var htmlStr = template("rightTContainer", resData);
		$(".rightTitle").html(htmlStr);
		var comid = getQueryVariable('com_id');
		var themeId = getQueryVariable('id');
		console.log(comid);
		console.log(themeId);
		if (!comid) {
//			$(".selectTitle").data("com_mid", comid).fadeIn();
//			return
			if(themeId){
	//			$(".selectTitle").fadeIn()
				$(".slc span").text("@主题" +themeId);
				$(".selectTitle").fadeOut();
//				return;
			}else{
				$(".selectTitle").fadeIn();
			}
		} else {
			$(".slc span").text("@" + comid);
			return;
		}

		

		
	})


	$("form").submit(function (event) {
		event.preventDefault();
		var data = new FormData(this);
		var message = $(".sContents").html();
		var themeId = getQueryVariable('id');
		console.log(message);
		var obj = {
			message:message,
		}
				
		 $.ajax({
		 	type:'post',
		 	url: pUrl,
		 	data: obj,	
		 	success: function (resData) {
		 		setTimeout(function() {
		 			if (resData.code == 1) {
		 				$(".rTitle").slideDown().text("哇槽...");
		 				location.href = "/index3.html";
		 			}
		 		}, 500);
				
		 	},
		 	error: function () {
		 		$(".rTitle").slideDown().text("吐槽失败，请检查您的网络是否断开");
		 		if (!$(".rTitle").data("open")) {
		 			$(".rTitle").data("open", true);
		 			setTimeout(function () {
		 				$(".rTitle").slideUp();
		 				$(".rTitle").data("open", false);
					}, 5000);
		 		}

		 	}
			
		 })
		$.ajax({
			type: "post",
			url: "/comment/upload?theme_id=" + themeId,
			data: data,
			cache: false,
			contentType: false,
			processData: false,
			success: function (response) {
				// console.log(response);
			}
		});
	});

	var obj = "";
	var pUrl = "";
	$(".send").click(function () {
		var forunId = $('.themeTitle').data('themed');
		var comid = getQueryVariable('com_id');
		var themeId = getQueryVariable('id');
		console.log(themeId);
		if($(".selectTitle").css("display")=="block"){
				if(!subForumId){
				$(".rTitle").slideDown().text("请选择主题");
				setTimeout(function(){
					$(".rTitle").slideUp()
				},2000)
				return;
			}
		}
	
//	console.log($(".sContents").html().length);
		if($(".sContents").html().length == 11){				
			$(".rTitle").slideDown().text("什么都没说 = =");
			setTimeout(function(){
				$(".rTitle").slideUp()
			},2000)
			return;
		}
		if (themeId) {
			if (comid) {
				// var themeId = $(".selectTitle").data("themeId");
				// var commid = $(".selectTitle").data("com_mid");
				pUrl = "/comment/reply?theme_id=" + themeId + "&comer_id=" + comid;
			} else {
				pUrl = "/comment/reply?theme_id=" + themeId;
			}
		} else {
			pUrl = "/comment/posting?forumId=" + subForumId;
			
		}

		console.log(pUrl);
		$("form").submit();
	});

	$(".sureI").on("click", function () {
		if ($(".ul img").attr("src")) {
			$(".imgBC img").attr("src", $(".ul img").attr("src"));
			$(".ul").addClass("animated rotateOut");
			setTimeout(navUp, 600);
			setTimeout(function () {
				$(".imgBC").show().removeClass("animated zoomOut").addClass("animated rotateIn");
				$(".ul").removeClass("animated rotateOut");
				$(".ul img").attr("src", " ");
			}, 1200);
		} else {
			$(".ul").addClass("animated rubberBand");
			setTimeout(function () {
				$(".ul").removeClass("animated rubberBand");
			}, 600);
		}

	});

	$(".imgBC span").on("click", function () {
		$(this).parent().removeClass("animated rotateIn").addClass("animated zoomOut").find("img").attr("src", "");

	});


	function getQueryVariable(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				return pair[1];
			}
		}
		return (false);
	}


	//	var vh = $(window).height();
	//	$(window).resize(function(){
	//		var eleh = $(window).height();
	//		$("footer").css("bottom",vh-eleh);
	//	});
	if($.cookie("permission") == 0){
		alert("您以被禁言");
		location.href = '/index3.html';
	}


});